package com.shiguiwu.springmybatis.spring.lifecycle.definition;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.ManagedSet;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @description:
 * ：通过api设置（Map、Set、List）属性
 * 下面我们来演示注入List、Map、Set，内部元素为普通类型及其他bean元素。
 * @author: stone
 * @date: Created by 2021/3/17 15:36
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.spring.lifecycle.definition
 */
public class DefinitionApiTests {

    public static void main(String[] args) {
        BeanDefinitionBuilder builder1 = BeanDefinitionBuilder.genericBeanDefinition(Pig.class.getName());
        BeanDefinitionBuilder builder2 = BeanDefinitionBuilder.genericBeanDefinition(Pig.class);
        BeanDefinition pig1 = builder1.addPropertyValue("name", "小猪佩奇")
                .addPropertyValue("age", 12)
                .getBeanDefinition();

        BeanDefinition pig2 = builder2.addPropertyValue("name", "乔治111")
                .addPropertyValue("age", 6)
                .getBeanDefinition();

        ManagedList<String> strings = new ManagedList<>();
        strings.addAll(Arrays.asList("字符串1", "strings","list"));
        ManagedSet<String> stringSet = new ManagedSet<>();
        stringSet.add("set");
        stringSet.add("注入set集合");

        ManagedMap<String, String> stringMap = new ManagedMap<>();
        stringMap.put("name", "小猪佩奇");
        stringMap.put("age", "13");

        List<RuntimeBeanReference> pigList = new ManagedList<>();
        pigList.addAll(Arrays.asList(new RuntimeBeanReference("pig1"), new RuntimeBeanReference("pig2")));

        Set<RuntimeBeanReference> pigSet = new ManagedSet<>();
        pigSet.add(new RuntimeBeanReference("pig1"));
        pigSet.add(new RuntimeBeanReference("pig2"));

        Map<String, RuntimeBeanReference> stringPigMap = new ManagedMap<>();
        stringPigMap.put("pig1", new RuntimeBeanReference("pig1"));
        stringPigMap.put("pig2", new RuntimeBeanReference("pig2"));

        BeanDefinition userDefinition = new GenericBeanDefinition();
        userDefinition.setBeanClassName(User.class.getName());
        userDefinition.getPropertyValues()
                .add("pig", new RuntimeBeanReference("pig1"))
                .add("name", "shiguiwu")
                .add("pigList", pigList)
                .add("pigSet", pigSet)
                .add("stringMap", stringMap)
                .add("stringPigMap", stringPigMap)
                .add("strings", strings)
                .add("stringSet", stringSet);
        DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
        factory.registerBeanDefinition("pig1", pig1);
        factory.registerBeanDefinition("pig2", pig2);
        factory.registerBeanDefinition("user", userDefinition);

        String[] beanDefinitionNames = factory.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(factory.getBean(beanDefinitionName));

        }

    }
}
